class JsonConverter {
  /// 将动态值转换为整数
  static int? toInt(dynamic value) {
    if (value == null) return null;
    if (value is int) return value;
    if (value is double) return value.toInt();
    if (value is String) {
      try {
        return int.parse(value);
      } catch (e) {
        return null;
      }
    }
    return null;
  }

  /// 将动态值转换为带默认值的整数
  static int toIntWithDefault(dynamic value, {int defaultValue = 0}) {
    return toInt(value) ?? defaultValue;
  }

  /// 将动态值转换为双精度浮点数
  static double? toDouble(dynamic value) {
    if (value == null) return null;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    if (value is String) {
      try {
        return double.parse(value);
      } catch (e) {
        return null;
      }
    }
    return null;
  }

  /// 将动态值转换为带默认值的双精度浮点数
  static double toDoubleWithDefault(dynamic value, {double defaultValue = 0.0}) {
    return toDouble(value) ?? defaultValue;
  }

  /// 将动态值转换为字符串
  static String? toStr(dynamic value) {
    if (value == null) return null;
    return value.toString();
  }

  /// 将动态值转换为带默认值的字符串
  static String toStrWithDefault(dynamic value, {String defaultValue = ''}) {
    if (value == null) return defaultValue;
    return value.toString();
  }

  /// 将动态值转换为布尔值
  static bool? toBool(dynamic value) {
    if (value == null) return null;
    if (value is bool) return value;
    if (value is int) return value != 0;
    if (value is String) {
      final lowercaseValue = value.toLowerCase();
      return lowercaseValue == 'true' || lowercaseValue == '1' || lowercaseValue == 'yes';
    }
    return null;
  }

  /// 将动态值转换为带默认值的布尔值
  static bool toBoolWithDefault(dynamic value, {bool defaultValue = false}) {
    return toBool(value) ?? defaultValue;
  }
} 